import mongodb from "@fastify/mongodb";
import multipart from "@fastify/multipart";
import staticPlugin from "@fastify/static";
import path from "path";
import { fileURLToPath } from "url";

export const registerPlugins = async (app) => {
  const __dirname = path.dirname(fileURLToPath(import.meta.url));

  // 注册 MongoDB 插件
  app.register(mongodb, {
    url: process.env.MONGODB_URI,
    database: process.env.MONGODB_DB,
  });

  // 注册文件上传插件
  await app.register(multipart, {
    limits: {
      fileSize: 10 * 1024 * 1024, // 限制文件大小为 10MB
      files: 1, // 限制同时上传的文件数量
    },
  });

  // 注册静态文件服务插件，用于提供上传的图片访问
  app.register(staticPlugin, {
    root: path.join(__dirname, "..", "..", "uploads"),
    prefix: "/uploads/",
  });
};
